package com.dd.census.service.impl;

import com.dd.census.entity.*;
import com.dd.census.mapper.*;
import com.dd.census.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 业务层实现类
 * ParameterServiceImpl
 * @author whn
 * @date 2021/03/13
 */
@Service
public class ParameterServiceImpl implements ParameterService {

    @Autowired
	ParameterMapper mapper;

    @Override
    public int insert(List<Parameter> parameter) {
		mapper.deleteAll();
        for(int i =0 ; i<parameter.size();i++){
			mapper.insert(parameter.get(i));
		}
    	return 1;
    }

    @Override
    public int update(Parameter parameter) {
    	return mapper.update(parameter);
    }

    @Override
    public int delete(Parameter parameter) {
    	return mapper.delete(parameter);
    }

	@Override
	public Parameter selectByKey(String key) {
		return mapper.selectByKey(key);
	}

	@Override
	public List<Parameter> selectList(Parameter parameter) {
		return mapper.selectList(parameter);
	}

	@Override
	public Map<String, Object> selectPage(Parameter parameter) {
		Map<String, Object> map = new HashMap<String, Object>();
		int total = this.total(parameter);
		map.put("pageCount", Math.ceil((double) total / parameter.getPageSize()));// 总页数
		map.put("total", total);// 总条数
		map.put("index", parameter.getPageIndex());// 当前页
		if (total > 0) {
			parameter.setPageIndex(parameter.getPageSize() * (parameter.getPageIndex() - 1));
			map.put("items", mapper.selectPage(parameter));
		}
		return map;
	}

	@Override
	public int total(Parameter parameter) {
		return mapper.total(parameter);
	}
}